<!--  -->
<template>
    <div>

    </div>
</template>

<script>


export default {

    data() {
        return {
            arr: [
                { id: 1, name: '留恋1', state: true, price: 20, count: 2 },
                { id: 2, name: '留恋2', state: false, price: 40, count: 1 },
                { id: 2, name: '留恋3', state: true, price: 10, count: 1 },
            ]

        }
    },
    created() {

    },
    mounted() {
        // this.t_foreach()
        //this.t_some()
      //  this.t_filter()
        this.t_reduce()

    },
    methods: {
        t_foreach() {
            //1；foreach 循环， 不会终止，  return 无作用
            this.arr.forEach(item => {
                console.log(item.name)
                if (item.name === '留恋1') {
                    console.log("次数：=" + item.name)
                    return true;
                }

            });

        },

        t_some() {
            //some 循环， 会终止，  return 有用
            this.arr.some(item => {
                console.log(item.name)
                if (item.name === '留恋1') {
                    console.log("次数：=" + item.name)
                    return true;
                }

            });

        },

        t_filter() {

            //过滤 false,只获取 true
            var log = this.arr.filter(item => item.state)
            console.log(log)

            // this.arr.filter(item => item.state).forEach(item => {
            //     console.log(item.name)
            //     if (item.name === '留恋1') {
            //         console.log("次数：=" + item.name)

            //     }

            // });

        },

        t_reduce() {

            //过滤 false,只获取 true
           // reduce(累加的结果, 当前循环)
            const result = this.arr.filter(item => item.state).reduce((ant, item) => {
             
                return ant += item.count * item.price
            },0);
            console.log(result)

        },
    }

}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>